package com.tianqu.tms.www.rental.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.RentalLocationInfo;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.rental.action.RentalLocationListAction;
import com.tianqu.tms.www.rental.actionform.RentalLocationAddInputForm;
import com.tianqu.tms.www.rental.viewhelper.RentalLocationViewHelper;

/**
 * 租位查询/修改-租位一览 controller
 * @author mufeiyu
 */
public class RentalLocationUpdateHistroySearchListController extends TmsController {
	private Logger logger = Logger.getLogger(RentalLocationUpdateHistroySearchListController.class);
	public static final String PAGING = "paging";
	public static final String EMPTY = "empty";
	
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Codes codes = Codes.getInstance();
			RentalLocationInfo rentalLocationInfo = RentalLocationInfo.getInstance();
			
			 if(uc.getAttribute(RentalLocationListAction.PAGENO) != null) {
				 rentalLocationInfo.setPageNo(Integer.parseInt((String)uc.getAttribute(RentalLocationListAction.PAGENO)));
				}
				if(uc.getAttribute(RentalLocationListAction.MAXINDEX) != null) {
					rentalLocationInfo.setMaxIndex(Integer.parseInt((String)uc.getAttribute(RentalLocationListAction.MAXINDEX)));
				}
				if(uc.getAttribute(RentalLocationListAction.SORTKEY) != null) {
					rentalLocationInfo.setSortKey(Integer.parseInt((String)uc.getAttribute(RentalLocationListAction.SORTKEY)));
				}
				if(uc.getAttribute(RentalLocationListAction.ASC) != null) {
					rentalLocationInfo.setAsc((new Boolean((String)uc.getAttribute(RentalLocationListAction.ASC))).booleanValue());
				}
			
			List<RentalLocationInfo> list = new ArrayList<RentalLocationInfo>();
			RentalLocationAddInputForm form = (RentalLocationAddInputForm)uc.getInputForm();
			// 判定翻页
			if (uc.getAttribute("seniorForm") != null) {
				form = (RentalLocationAddInputForm) uc.getAttribute("seniorForm");
			}

			String[] handleType = form.getHandleType();
			
			list = rentalLocationInfo.searchRentalLocationUpdateHistroy(form.getLocationSerial(),form.getHandleName(),
					gethandleType(handleType),form.getHandleDateS(),form.getHandleDateE());
					
			RentalLocationViewHelper viewh = new RentalLocationViewHelper();
			//插入操作员
			User user = User.getInstance();
			
			//查询结果判断
			if (null != list && list.size() > 0) {
				List<RentalLocationViewHelper> rentalLocationViewHelperlist = new ArrayList<RentalLocationViewHelper>();				
				for (int i = 0; i <list.size(); i++) {
					RentalLocationInfo locationInfo = new RentalLocationInfo();
					RentalLocationViewHelper rentalLocationViewHelper = new RentalLocationViewHelper();
					locationInfo = list.get(i);
					rentalLocationViewHelper.setId(locationInfo.getId());
					rentalLocationViewHelper.setRowNo(locationInfo.getRowNo());
					rentalLocationViewHelper.setLocationSerial(locationInfo.getLocationSerial());
					user = user.getUserInfoById(StringUtil.getint(locationInfo.getHandleName()));
					rentalLocationViewHelper.setHandleTypeName(locationInfo.getHandleTypeName());
					rentalLocationViewHelper.setRemark(locationInfo.getRemark());
					rentalLocationViewHelper.setHandleName(user.getRealName());
					rentalLocationViewHelper.setHandleDate(locationInfo.getHandleDate());
					rentalLocationViewHelper.setCssFlag(locationInfo.getCssFlag());
					rentalLocationViewHelperlist.add(rentalLocationViewHelper);
				}
				viewh.setRentalLocationUpdateHistroyList(rentalLocationViewHelperlist);				
			}
			uc.setAttribute(TmsConstants.VIEWHELP, viewh);
			uc.setAttribute("seniorForm", form);
			uc.setAttribute(PAGING, rentalLocationInfo);
		 			
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
	/**
	 * 合同状态取得
	 * @return
	 */
	public String gethandleType(String[] handleType){
		String strHandleType = "";
		
		if(null != handleType &&handleType.length > 0){
			for (int i = 0; i < handleType.length; i++){
				if (i == 0) {
					strHandleType = handleType[i];
				} else {
					strHandleType = strHandleType + "," +handleType[i];
				}
			}
		}
		
		return strHandleType;
	}
}